<?php

/*

Quests (quests.pro.php)

*/
ob_start();
$page_title = "Quests";
$rank_check = 1;
include "header.inc.php";

$findSetup = fetch("SELECT * FROM quest_items WHERE game = '$game'");
if (!$findSetup[id])
{
	if ($rank == 7)
	{
		$ifAdmin = "<br><b>Admins:</b> Click <a href=admin/admin_setup_quests.php?game=$game>here</a> to setup this game!<br>";
	}

	die("This game has not been setup to play yet. Sorry.$ifAdmin$closeHTML");
}

$findQuest = fetch("SELECT * FROM quests WHERE game = '$game' AND user = '$userid'");

if ($act == "start")
{
	if ($findQuest[id])
	{
		die("$openHTML<p>You are already in a quest!</p>$closeHTML");
	}

	if ($diff == "easy")
	{
		$explode = explode(", ", $findSetup[easy_i]);
		$countExplode = count($explode) - 1;
		$endTime = $timestamp + rand(3600,10800);
	}
	if ($diff == "med")
	{
		$explode = explode(", ", $findSetup[med_i]);
		$countExplode = count($explode) - 1;
		$endTime = $timestamp + rand(1800,5400);
	}
	if ($diff == "hard")
	{
		$explode = explode(", ", $findSetup[hard_i]);
		$countExplode = count($explode) - 1;
		$endTime = $timestamp + rand(900,2700);
	}

	$oneToThree = rand(1,3);
	for ($x = 1; $x <= $oneToThree; $x++)
	{
		$randItem = rand(0,$countExplode);
		if ($x < $oneToThree) { $puntuation[$x] = ", "; }
		$getItems .= "$explode[$randItem]$puntuation[$x]";
	}
	mysql_query("INSERT INTO quests (game,difficulty,items_needed,end_time,user) VALUES ('$game','$diff','$getItems','$endTime','$userid')");
	header(error("quests.php?game=$game","Your quest has been started!"));
}

if ($act == "end")
{
	$haveItems = 0;
	$explodeItems = explode(", ", $findQuest[items_needed]);
	$countItems = count($explodeItems);

	if ($timestamp >= $findQuest[end_time])
	{
		die("$openHTML<p align=center><b>You have run out of time already! <a href=quests.php?game=$game>Try again!</a></b></p>$closeHTML");
		mysql_query("DELETE FROM quests WHERE game = '$game' AND user = '$userid'");
	}


	for ($x = 0; $x <= $countItems; $x++)
	{
		$findItem = fetch("SELECT * FROM items2 WHERE item_name = '$explodeItems[$x]' AND game = '$game'");
		$bringItems .= "<td><p align=center><img src=images/user_images/opg_$game/items/item_$findItem[id].gif><br>$findItem[item_name]</p></td>";
		$findYourItem = fetch("SELECT id FROM usersitems2 WHERE owner = '$userid' AND game = '$game' AND item_id = '$findItem[id]' ORDER BY id LIMIT 1");
		$yourItemID = $findYourItem[id];
		$item[$x] = $findYourItem[id];

		if ((!$findYourItem[id]) OR ($taken[$yourItemID]))
		{
			$findYourItem2 = fetch("SELECT id FROM usersitems2 WHERE owner = '$userid' AND game = '$game' AND item_id = '$findItem[id]' ORDER BY id LIMIT 1,2");
			if ($findYourItem2[id])
			{
				$yourItemID = $findYourItem2[id];
				$item[$x] = $findYourItem2[id];

			}
			if (!$findYourItem2[id])
			{
				$findYourItem3 = fetch("SELECT id FROM usersitems2 WHERE owner = '$userid' AND game = '$game' AND item_id = '$findItem[id]' ORDER BY id LIMIT 1,3");
				if ($findYourItem3[id])
				{
					$yourItemID = $findYourItem3[id];
					$item[$x] = $findYourItem3[id];

				}
			}
		}
		if (!$taken[$yourItemID])
		{
			if ($findYourItem[id])
			{
				$haveItems++;
				$taken[$yourItemID] = $yourItemID;
			}
		}
	}

	if ($haveItems == $countItems)
	{
		for ($x = 0; $x <= $countItems; $x++)
		{
			mysql_query("DELETE FROM usersitems2 WHERE id = '$item[$x]' AND owner = '$userid' AND game = '$game'");
		}

		if ($findQuest[difficulty] == "easy")
		{
			$explode_p = explode(", ", $findSetup[easy_p]);
			$explodePts = explode(",", $findSetup[easy_pts]);
			$definite = $findSetup[def_e];
			$score = 1;
		}
		if ($findQuest[difficulty] == "med")
		{
			$explode_p = explode(", ", $findSetup[med_p]);
			$explodePts = explode(",", $findSetup[med_pts]);
			$definite = $findSetup[def_i];
			$score = 2;
		}
		if ($findQuest[difficulty] == "hard")
		{
			$explode_p = explode(", ", $findSetup[hard_p]);
			$explodePts = explode(",", $findSetup[hard_pts]);
			$definite = $findSetup[def_h];
			$score = 3;
		}

		$countExplode = count($explode_p) - 1;

		$oneToThree = rand(1,3);
		for ($x = 1; $x <= $oneToThree; $x++)
		{
			$randItem = rand(0,$countExplode);
			if ($x < $oneToThree) { $puntuation[$x] = ", "; }
			$giveitems .= "$explode_p[$randItem]$puntuation[$x]";
		}
		$explodeItems = explode(", ", $giveitems);
		$countItems = count($explodeItems) - 1;

		if ($countItems == 0) { $word = "this"; } else { $word = "these"; }

		print "$openHTML<p align=center>Thank you for completing the quest, please take $word!! ";

		for ($x = 0; $x <= $countItems; $x++)
		{
			$findItem = fetch("SELECT * FROM items2 WHERE item_name = '$explodeItems[$x]' AND 	game = '$game'");
			$item[$x] = $findItem[id];
			$itemparts[$x] = $findItem[parts];

			$bringItems3 .= "<td><p align=center><img 	src=images/user_images/opg_$game/items/item_$findItem[id].gif><br>$findItem[item_name]</p></td>";
		}

		for ($x = 0; $x <= $countItems; $x++)
		{
			mysql_query("INSERT INTO usersitems2 (owner,item_id,parts_left,game) VALUES ('$userid','$item[$x]','$itemparts[$x]','$game')");
		}

		if ($definite)
		{
			$findItem = fetch("SELECT id,parts FROM items2 WHERE item_name = '$definite' AND game = '$game'");
			mysql_query("INSERT INTO usersitems2 (owner,item_id,parts_left,game) VALUES ('$userid','$findItem[id]','$findItem[parts]','$game')");

			$bringItems4 = "<p align=center>And for beating this $findQuest[difficulty] quest you get this prize:<br><br><img src=images/user_images/opg_$game/items/item_$findItem[id].gif><br>$definite</p>";
		}

		$randPoints = rand($explodePts[0],$explodePts[1]);

		points($game,$username,$randPoints);

		print "<p align=center><table><tr>$bringItems3</tr></table>$bringItems4";

		print "<p align=center>Here, you can also have $randPoints $pointsVar!</p>";

		print "<p align=center><a href=quests.php?game=$game>Play Again!</a> | <a href=games.php?game=$game>Leave</a></p>";

		print "$closeHTML";

		mysql_query("DELETE FROM quests WHERE game = '$game' AND user = '$userid'");
		$findHS = fetch("SELECT * FROM highscores WHERE game = '$game' AND user = '$userid' AND game_name = 'Quests'");
		if (!$findHS[id])
		{
			mysql_query("INSERT INTO highscores (game, user, game_name, score) VALUES ('$game', '$userid', 'Quests', '$score')");
		}
		if ($findHS[id])
		{
			mysql_query("UPDATE highscores SET score=$findHS[score]+$score WHERE game = '$game' AND user = '$userid' AND game_name = 'Quests'");
		}

		$findHS2 = fetch("SELECT * FROM highscores WHERE game = '$game' AND user = '$userid' AND game_name = 'Quests - Daily' AND daily = '$datestamp'");
		if (!$findHS2[id])
		{
			mysql_query("INSERT INTO highscores (game, user, game_name, score, daily) VALUES ('$game', '$userid', 'Quests - Daily', '$score', '$datestamp')");
		}
		if ($findHS2[id])
		{
			mysql_query("UPDATE highscores SET score=$findHS2[score]+$score WHERE game = '$game' AND user = '$userid' AND game_name = 'Quests - Daily' AND daily = '$datestamp'");
		}
	}

	if ($haveItems != $countItems)
	{
		header(error("quests.php?game=$game","Your quest has not been completed yet! Only $haveItems out of $countItems have been brought to me!"));
	}
}

if ($act == "giveup")
{
	if ($findQuest[difficulty] == "easy") { $score = 1; }
	if ($findQuest[difficulty] == "med") { $score = 2; }
	if ($findQuest[difficulty] == "hard") { $score = 3; }

	$wait_til = $timestamp + ($score * 900);
	mysql_query("DELETE FROM quests WHERE game = '$game' AND user = '$userid'");
	mysql_query("INSERT INTO gave_up (game,user,game_name,wait_til) VALUES ('$game','$userid','Quests','$wait_til')");
	header(error("quests.php?game=$game","You have given up on the quest!"));
}

?>